/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exmtgproj;

import java.io.Serializable;
import java.util.ArrayList;

/**
 * NYI cost och spellText används av Preview för att skriva ut texten på kortet
 * @author marcus ahlen
 */
public class Ability implements Targetable, Statics, Serializable {

    boolean manaAbility;
    boolean slow;       //(fast: flash, instant, abilities)
    byte[] cost = new byte[COLORS];
    String spellText = "";
    CardList cards = new CardList();
    byte zone;
    byte type;
    boolean xCost = false;
    ArrayList<AbilityEvent> events = new ArrayList<AbilityEvent>();
    Targetable[] eventTargets = new Targetable[0];
    ArrayList<AbilityEvent> costs = new ArrayList<AbilityEvent>();
    Targetable[] costTargets = new Targetable[0];

    /**
     *
     * @param c the card that's trying to put the spell on the stack
     * @return true if it's possible
     */
    public boolean canPutOnStack(Card c) {
        ArrayList<Targetable> targets = new ArrayList<Targetable>();
        for (AbilityEvent ae : events) {
            Targetable[] t = ae.target();
            if (t == null) {
                return false;
            }
            for (Targetable tar : t) {
                targets.add(tar);
            }
        }
        eventTargets = new Targetable[targets.size()];
        for (int i = 0; i < targets.size(); i++) {

            eventTargets[i] = targets.get(i);
        }
        targets.clear();
        for (AbilityEvent ae : costs) {
            Targetable[] t = ae.target();
            if (t == null) {
                return false;
            }
            for (Targetable tar : t) {
                targets.add(tar);
            }
        }
        eventTargets = new Targetable[targets.size()];
        for (int i = 0; i < targets.size(); i++) {
        }
        /*byte[] payed = Game.players.get(c.controller).pay(cost);
        if (!costPayed(cost, payed)) {
        return false;
        } else {
        }
        return true;*/ return Game.players.get(c.controller).pay(cost);
    }

    public void resolve(Card c) {
        for (AbilityEvent ae : events) {
            ae.resolve(c, eventTargets);
        }
    }

    static public boolean costPayed(byte[] cost, byte[] payed) {
        int cmc = cost[0];
        int pay = payed[0];
        for (int i = 1; i < COLORS; i++) {
            if (cost[i] > payed[i]) {
                cmc += cost[i];
                pay += payed[i];
                return false;
            }
        }
        if (cmc > pay) {
            return false;
        }
        return true;
    }
    /*abstract void play();
    abstract void hand();

    abstract void stack();

    abstract void enters();

    abstract void attack(byte player);

    abstract void block(byte target);

    abstract void leavePlay();

    abstract void grave();

    abstract boolean destroy(Card card, Card by);
    private boolean destroy(Card by, boolean canRegenerate){
    if(regenerated&&canRegenerate){
    return false;
    }else{
    return true;
    }
    }*/

    /*abstract void staticAbillity();

    abstract void triggeredAbillity();

    abstract void activatedAbillity();*/
}
